#ifndef _THREAD_H_
#define _THREAD_H_

#include <iostream>
#include <string>
#include <pthread.h>
#include <cstdio>
#include <cstring>
#include <functional>

namespace ThreadModlue
{
    static uint32_t number = 1;

    
    class Thread    
    {
        //#define function<void()> func_t; //暂时这样写，完全够了
        //缺点就是不能随意的 添加 我们想要的 线程运行函数的 的参数
        using func_t = std::function<void()>;
        private:
        void EnableDetach()
        {
            //std::cout << "线程被分离了" << std::endl;
            _isdetach = true;
        }
        void EnableRunning()
        {
            //std::cout << "线程被启动了" << std::endl;
            _isrunning = true;
        }

        static void* Routine(void* args) // 属于类内的成员函数，默认包含this指针！
        {
            Thread *self = static_cast<Thread *> (args);
            self->EnableRunning();

            if(self->_isdetach)
                self->Detach();

            pthread_setname_np(self->_tid, self->_name.c_str());  //线程局部栈
            self->_func(); //回调函数;
            return nullptr;
        }

    public:
        Thread(func_t func)
            : _tid(0),
            _isdetach(false), 
            _isrunning(false),
            res(nullptr),
            _func(func)
        {
            _name = "thread-" + std::to_string(number++);
        }
        
        void Detach()
        {
            if(_isdetach)
            return;

            if(_isrunning)
                pthread_detach(_tid);

            EnableDetach();
        }

        bool Start()
        {
            if (_isrunning)
                return false;

            int n = pthread_create(&_tid, nullptr, Routine, this);
            if (n != 0)
            {
                //std::cerr << "create thread error: " << strerror(n) << std::endl;
                return false;
            }
            else
            {
                //std::cout << "success : " << _name << std::endl;
                return true;
            }
        }

        bool Stop()
        {
            if (_isrunning)
            {
                int n = pthread_cancel(_tid);
                if (n != 0)
                {
                    //std::cerr << "cancel thread error: " << strerror(n) << std::endl;
                    return false;
                }
                else
                {
                    _isrunning = false;
                    //std::cout << _name << " cancle success" << std::endl;
                    return true;
                }
            }
            return false;
        }

        void Join()
        {
            //std::cout << "11111111" << std::endl;
            //std::cout << _isrunning << std::endl;
                if (_isdetach)
                {
                    //std::cout << "此线程已经是分离的了,不能进行join" << std::endl;
                    return;
                }

                int n = pthread_join(_tid, &res);
                //std::cout << "******n********" << std::endl; 
                if (n != 0)
                {
                    //std::cerr << "join thread error: " << strerror(n) << std::endl;
                }
                else
                {
                    //std::cout << "join success" << std::endl;
                }
        }


        pthread_t Id()
        {
            return _tid;
        }

        ~Thread()
        {
        }

    private:
        std::string _name;
        pthread_t _tid;
        func_t _func;

        void *res;
        bool _isdetach; 
        bool _isrunning;
    };
}

#endif